﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class BangLuongBO : Csla.BusinessBase<BangLuongBO>
    {
        private Hashtable hstOldValue = new Hashtable();
        public Hashtable HstOldValue
        {
            get { return this.hstOldValue; }
            set { this.hstOldValue = value; }
        }

        #region Business Properties

        private int? _BangLuongID = null;
        public int? BangLuongID
        {
            get { return this._BangLuongID; }
            set { this._BangLuongID = value; PropertyHasChanged("BangLuongID"); }
        }

        private int? _ThangTinhLuongID = null;
        public int? ThangTinhLuongID
        {
            get { return this._ThangTinhLuongID; }
            set { this._ThangTinhLuongID = value; PropertyHasChanged("ThangTinhLuongID"); }
        }

        private int? _Nam = null;
        public int? Nam
        {
            get { return this._Nam; }
            set { this._Nam = value; PropertyHasChanged("Nam"); }
        }

        private int? _NhanVienID = null;
        public int? NhanVienID
        {
            get { return this._NhanVienID; }
            set { this._NhanVienID = value; PropertyHasChanged("NhanVienID"); }
        }

        private int? _PhongBanID = null;
        public int? PhongBanID
        {
            get { return this._PhongBanID; }
            set { this._PhongBanID = value; PropertyHasChanged("PhongBanID"); }
        }

        private int? _ChucVuID = null;
        public int? ChucVuID
        {
            get { return this._ChucVuID; }
            set { this._ChucVuID = value; PropertyHasChanged("ChucVuID"); }
        }

        private double? _Luong = 0;
        public double? Luong
        {
            get
            {
                if (this._Luong != null)
                    if (this._Luong == 0)
                        return null;

                return this._Luong;
            }
            set { this._Luong = value; PropertyHasChanged("Luong"); }
        }

        private double? _PhuCap = 0;
        public double? PhuCap
        {
            get
            {
                if (this._PhuCap != null)
                    if (this._PhuCap == 0)
                        return null;

                return this._PhuCap;
            }
            set { this._PhuCap = value; PropertyHasChanged("PhuCap"); }
        }

        private double? _PhuCapBHXH = 0;
        public double? PhuCapBHXH
        {
            get
            {
                if (this._PhuCapBHXH != null)
                    if (this._PhuCapBHXH == 0)
                        return null;

                return this._PhuCapBHXH;
            }
            set { this._PhuCapBHXH = value; PropertyHasChanged("PhuCapBHXH"); }
        }

        private double? _ThuNhapKhac = 0;
        public double? ThuNhapKhac
        {
            get
            {
                if (this._ThuNhapKhac != null)
                    if (this._ThuNhapKhac == 0)
                        return null;

                return this._ThuNhapKhac;
            }
            set { this._ThuNhapKhac = value; PropertyHasChanged("ThuNhapKhac"); }
        }

        private double? _Tong = 0;
        public double? Tong
        {
            get
            {
                if (this._Tong != null)
                    if (this._Tong == 0)
                        return null;

                return this._Tong;
            }
            set { this._Tong = value; PropertyHasChanged("Tong"); }
        }

        private double? _TamUng = 0;
        public double? TamUng
        {
            get
            {
                if (this._TamUng != null)
                    if (this._TamUng == 0)
                        return null;

                return this._TamUng;
            }
            set { this._TamUng = value; PropertyHasChanged("TamUng"); }
        }

        private double? _BHXH = 0;
        public double? BHXH
        {
            get
            {
                if (this._BHXH != null)
                    if (this._BHXH == 0)
                        return null;

                return this._BHXH;
            }
            set { this._BHXH = value; PropertyHasChanged("BHXH"); }
        }

        private double? _BHYT = 0;
        public double? BHYT
        {
            get
            {
                if (this._BHYT != null)
                    if (this._BHYT == 0)
                        return null;

                return this._BHYT;
            }
            set { this._BHYT = value; PropertyHasChanged("BHYT"); }
        }

        private double? _BHTN = 0;
        public double? BHTN
        {
            get
            {
                if (this._BHTN != null)
                    if (this._BHTN == 0)
                        return null;

                return this._BHTN;
            }
            set { this._BHTN = value; PropertyHasChanged("BHTN"); }
        }

        private double? _Khac = 0;
        public double? Khac
        {
            get
            {
                if (this._Khac != null)
                    if (this._Khac == 0)
                        return null;

                return this._Khac;
            }
            set { this._Khac = value; PropertyHasChanged("Khac"); }
        }

        private double? _ThueTNCN = 0;
        public double? ThueTNCN
        {
            get
            {
                if (this._ThueTNCN != null)
                    if (this._ThueTNCN == 0)
                        return null;

                return this._ThueTNCN;
            }
            set { this._ThueTNCN = value; PropertyHasChanged("ThueTNCN"); }
        }

        private double? _ThucNhan = 0;
        public double? ThucNhan
        {
            get
            {
                if (this._ThucNhan != null)
                    if (this._ThucNhan == 0)
                        return null;

                return this._ThucNhan;
            }
            set { this._ThucNhan = value; PropertyHasChanged("ThucNhan"); }
        }

        private int? _USRC = Session.LoginID;
        public int? USRC
        {
            get { return this._USRC; }
            set { this._USRC = value; PropertyHasChanged("USRC"); }
        }

        private DateTime? _DATC = DateTime.MaxValue;
        public DateTime? DATC
        {
            get { return this._DATC; }
            set { this._DATC = value; PropertyHasChanged("DATC"); }
        }

        private int? _USRM = null;
        public int? USRM
        {
            get { return this._USRM; }
            set { this._USRM = value; PropertyHasChanged("USRM"); }
        }

        private DateTime? _DATM = null;
        public DateTime? DATM
        {
            get { return this._DATM; }
            set { this._DATM = value; PropertyHasChanged("DATM"); }
        }

        private bool? _DELE = false;
        public bool? DELE
        {
            get { return this._DELE; }
            set { this._DELE = value; PropertyHasChanged("DELE"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        private string _MaNhanVien = null;
        public string MaNhanVien
        {
            get { return this._MaNhanVien; }
            set { this._MaNhanVien = value; }
        }

        private string _Ho = null;
        public string Ho
        {
            get { return this._Ho; }
            set { this._Ho = value; }
        }

        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; }
        }

        private bool? _Nu = false;
        public bool? Nu
        {
            get { return this._Nu; }
            set { this._Nu = value; }
        }

        protected override object GetIdValue()
        {
            return _BangLuongID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newBangLuongID = -1;
        private BangLuongBO()
        {
            _BangLuongID = newBangLuongID--;
        }

        public BangLuongBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public BangLuongBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static BangLuongBO NewRootBangLuongBO()
        {
            return DataPortal.Create<BangLuongBO>(new RootCriteria());
        }

        internal static BangLuongBO NewChildBangLuongBO()
        {
            return DataPortal.Create<BangLuongBO>(new ChildCriteria());
        }

        public static BangLuongBO GetRootBangLuongBO(int _BangLuongID)
        {
            return DataPortal.Fetch<BangLuongBO>(new RootCriteria(_BangLuongID));
        }

        public static BangLuongBO GetRootBangLuongBOByWhere(string where)
        {
            return DataPortal.Fetch<BangLuongBO>(where);
        }

        internal static BangLuongBO GetChildBangLuongBO(SqlDataReader dataReader)
        {
            return new BangLuongBO(dataReader);
        }

        public static void DeleteBangLuongBO(int _BangLuongID)
        {
            DataPortal.Delete(new RootCriteria(_BangLuongID));
        }

        public static void DeleteBangLuongBO(int _BangLuongID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_BangLuongID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int BangLuongID;
            public bool directDelete = false;
            public RootCriteria(int _BangLuongID)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = false;
            }

            public RootCriteria(int _BangLuongID, bool directDelete)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int BangLuongID;
            public bool directDelete = false;
            public ChildCriteria(int _BangLuongID)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = false;
            }

            public ChildCriteria(int _BangLuongID, bool directDelete)
            {
                this.BangLuongID = _BangLuongID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("BangLuongBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[BangLuongID] = " + criteria.BangLuongID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("BangLuongBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["BangLuongID"] is DBNull))
                this._BangLuongID = (int)dataReader["BangLuongID"];
            if (!(dataReader["ThangTinhLuongID"] is DBNull))
                this._ThangTinhLuongID = (int)dataReader["ThangTinhLuongID"];
            if (!(dataReader["Nam"] is DBNull))
                this._Nam = (int)dataReader["Nam"];
            if (!(dataReader["NhanVienID"] is DBNull))
                this._NhanVienID = (int)dataReader["NhanVienID"];
            if (!(dataReader["PhongBanID"] is DBNull))
                this._PhongBanID = (int)dataReader["PhongBanID"];
            if (!(dataReader["ChucVuID"] is DBNull))
                this._ChucVuID = (int)dataReader["ChucVuID"];
            if (!(dataReader["Luong"] is DBNull))
                this._Luong = (double)dataReader["Luong"];
            if (!(dataReader["PhuCap"] is DBNull))
                this._PhuCap = (double)dataReader["PhuCap"];
            if (!(dataReader["PhuCapBHXH"] is DBNull))
                this._PhuCapBHXH = (double)dataReader["PhuCapBHXH"];
            if (!(dataReader["ThuNhapKhac"] is DBNull))
                this._ThuNhapKhac = (double)dataReader["ThuNhapKhac"];
            if (!(dataReader["Tong"] is DBNull))
                this._Tong = (double)dataReader["Tong"];
            if (!(dataReader["TamUng"] is DBNull))
                this._TamUng = (double)dataReader["TamUng"];
            if (!(dataReader["BHXH"] is DBNull))
                this._BHXH = (double)dataReader["BHXH"];
            if (!(dataReader["BHYT"] is DBNull))
                this._BHYT = (double)dataReader["BHYT"];
            if (!(dataReader["BHTN"] is DBNull))
                this._BHTN = (double)dataReader["BHTN"];
            if (!(dataReader["Khac"] is DBNull))
                this._Khac = (double)dataReader["Khac"];
            if (!(dataReader["ThueTNCN"] is DBNull))
                this._ThueTNCN = (double)dataReader["ThueTNCN"];
            if (!(dataReader["ThucNhan"] is DBNull))
                this._ThucNhan = (double)dataReader["ThucNhan"];
            if (!(dataReader["USRC"] is DBNull))
                this._USRC = (int)dataReader["USRC"];
            if (!(dataReader["DATC"] is DBNull))
                this._DATC = (DateTime)dataReader["DATC"];
            if (!(dataReader["USRM"] is DBNull))
                this._USRM = (int)dataReader["USRM"];
            if (!(dataReader["DATM"] is DBNull))
                this._DATM = (DateTime)dataReader["DATM"];
            if (!(dataReader["DELE"] is DBNull))
                this._DELE = (bool)dataReader["DELE"];

            GetOldData();
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._USRC = Session.LoginID;
            this._DATC = DateTime.MaxValue;
            this._USRM = null;
            this._DATM = null;
            this._DELE = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("BangLuongBO_InsertBO");
            db.AddInParameter(cmd, "@ThangTinhLuongID", SqlDbType.Int, this._ThangTinhLuongID);
            db.AddInParameter(cmd, "@Nam", SqlDbType.Int, this._Nam);
            db.AddInParameter(cmd, "@NhanVienID", SqlDbType.Int, this._NhanVienID);
            db.AddInParameter(cmd, "@PhongBanID", SqlDbType.Int, this._PhongBanID);
            db.AddInParameter(cmd, "@ChucVuID", SqlDbType.Int, this._ChucVuID);
            db.AddInParameter(cmd, "@Luong", SqlDbType.Float, this._Luong);
            db.AddInParameter(cmd, "@PhuCap", SqlDbType.Float, this._PhuCap);
            db.AddInParameter(cmd, "@PhuCapBHXH", SqlDbType.Float, this._PhuCapBHXH);
            db.AddInParameter(cmd, "@ThuNhapKhac", SqlDbType.Float, this._ThuNhapKhac);
            db.AddInParameter(cmd, "@Tong", SqlDbType.Float, this._Tong);
            db.AddInParameter(cmd, "@TamUng", SqlDbType.Float, this._TamUng);
            db.AddInParameter(cmd, "@BHXH", SqlDbType.Float, this._BHXH);
            db.AddInParameter(cmd, "@BHYT", SqlDbType.Float, this._BHYT);
            db.AddInParameter(cmd, "@BHTN", SqlDbType.Float, this._BHTN);
            db.AddInParameter(cmd, "@Khac", SqlDbType.Float, this._Khac);
            db.AddInParameter(cmd, "@ThueTNCN", SqlDbType.Float, this._ThueTNCN);
            db.AddInParameter(cmd, "@ThucNhan", SqlDbType.Float, this._ThucNhan);
            db.AddInParameter(cmd, "@USRC", SqlDbType.Int, this._USRC);
            db.AddOutParameter(cmd, "@NewBangLuongID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDATC", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._BangLuongID = (int)cmd.Parameters["@NewBangLuongID"].Value;
            this._DATC = (DateTime)cmd.Parameters["@NewDATC"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._USRM = Session.LoginID;
            this._DATM = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("BangLuongBO_UpdateBO");
            db.AddInParameter(cmd, "@BangLuongID", SqlDbType.Int, this._BangLuongID);
            db.AddInParameter(cmd, "@ThangTinhLuongID", SqlDbType.Int, this._ThangTinhLuongID);
            db.AddInParameter(cmd, "@Nam", SqlDbType.Int, this._Nam);
            db.AddInParameter(cmd, "@NhanVienID", SqlDbType.Int, this._NhanVienID);
            db.AddInParameter(cmd, "@PhongBanID", SqlDbType.Int, this._PhongBanID);
            db.AddInParameter(cmd, "@ChucVuID", SqlDbType.Int, this._ChucVuID);
            db.AddInParameter(cmd, "@Luong", SqlDbType.Float, this._Luong);
            db.AddInParameter(cmd, "@PhuCap", SqlDbType.Float, this._PhuCap);
            db.AddInParameter(cmd, "@PhuCapBHXH", SqlDbType.Float, this._PhuCapBHXH);
            db.AddInParameter(cmd, "@ThuNhapKhac", SqlDbType.Float, this._ThuNhapKhac);
            db.AddInParameter(cmd, "@Tong", SqlDbType.Float, this._Tong);
            db.AddInParameter(cmd, "@TamUng", SqlDbType.Float, this._TamUng);
            db.AddInParameter(cmd, "@BHXH", SqlDbType.Float, this._BHXH);
            db.AddInParameter(cmd, "@BHYT", SqlDbType.Float, this._BHYT);
            db.AddInParameter(cmd, "@BHTN", SqlDbType.Float, this._BHTN);
            db.AddInParameter(cmd, "@Khac", SqlDbType.Float, this._Khac);
            db.AddInParameter(cmd, "@ThueTNCN", SqlDbType.Float, this._ThueTNCN);
            db.AddInParameter(cmd, "@ThucNhan", SqlDbType.Float, this._ThucNhan);
            db.AddInParameter(cmd, "@USRM", SqlDbType.Int, this._USRM);
            db.AddInParameter(cmd, "@DELE", SqlDbType.Bit, this._DELE);

            db.AddOutParameter(cmd, "@NewDATM", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DATM = (DateTime)cmd.Parameters["@NewDATM"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_BangLuongID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("BangLuongBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@BangLuongID", SqlDbType.Int, criteria.BangLuongID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        private void GetOldData()
        {
            Type t = this.GetType();
            MemberInfo[] mi = t.GetMembers();
            int length = mi.Length;
            string name = "";
            object objValue = null;
            string strValue = null;

            for (int i = 0; i < length; i++)
                if (mi[i].MemberType == MemberTypes.Property)
                {
                    name = mi[i].Name;
                    if (StaticUtil.Log_ValidColumn(name))
                    {
                        objValue = t.GetProperty(name).GetValue(this, null);
                        if (objValue != null)
                            strValue = objValue.ToString();
                        else
                            strValue = "";

                        hstOldValue.Add(name, strValue);
                    }
                }
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_BangLuongID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
